#include <string> //for string varables
#include <fstream> //for file I/O
#include <iostream> // for some termnial stuff
#include <stdlib.h> //for exit funct
#include <GL/glut.h> //for OpenGL and GLUT

using namespace std; //For ease of typeing (no std::cout, just cout)

void initRendering() {
	//Makes 3D drawing work when something is in front of something else
	glEnable(GL_DEPTH_TEST);
}
void handleResize(int w, int h) {
	//Tell OpenGL how to convert from coordinates to pixel values
	glViewport(0, 0, w, h);
	
	glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
	
	//Set the camera perspective
	glLoadIdentity(); //Reset the camera
	gluPerspective(45.0,                  //The camera angle
				   (double)w / (double)h, //The width-to-height ratio
				   1.0,                   //The near z clipping coordinate
				   200.0);                //The far z clipping coordinate
}

void drawScene() {
	//Clear information from last draw
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	glMatrixMode(GL_MODELVIEW); //Switch to the drawing perspective
	glLoadIdentity(); //Reset the drawing perspective

	//start deciding what to draw. The way I do it will probly lead to high frame rates,
	// (reading a file every time i want to draw) but it is ok for now.
	//later probly we will make a array with every draw command, or something.

ifstream script_file ( "mainscript.txt" );


  string scriptline;
  string end;

  end = "later";

	  while (end == "later") {
  script_file>> scriptline;

if (scriptline == "end") {

    end = "now";
    cout<< "end dected, ending\n\n press enter";
  script_file.close();
  ifstream script_file ( "mainscript.txt" );
}

if (scriptline == "pause") {

    cout<< "Pause dected, starting infunit loop";
	while (1) {}
}

if (scriptline == "maketriangle") {

//stick triangle code here
	glBegin(GL_TRIANGLES);

	glVertex3f(-0.5f, 0.5f, -5.0f);
	glVertex3f(-1.0f, 1.5f, -5.0f);
	glVertex3f(-1.5f, 0.5f, -5.0f);
	
	glEnd(); //End triangle coordinates
}
//end triangle
}
	
 	glutSwapBuffers(); //draw
}
void handleKeypress(unsigned char key, //The key that was pressed
					int x, int y) {    //The current mouse coordinates
	switch (key) {
		case 27: //Escape key
			exit(0); //Exit the program
	}
}
int main(int argc, char** argv) //no idea what int argc ect is, but apparently glut needs it
{


	//Initialize GLUT
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glutInitWindowSize(400, 400); //Set the window size
	
	//Create the window
	glutCreateWindow("Basic Shapes - videotutorialsrock.com");
	initRendering(); //Initialize rendering
	
	//Set handler functions for drawing, keypresses, and window resizes
	glutDisplayFunc(drawScene);
	glutKeyboardFunc(handleKeypress);
	glutReshapeFunc(handleResize);

cout<< "----------Starting Main Loop--------\n\n\n";

glutMainLoop(); // let glut take care of our problems... (never returns,
//just keeps going, calling our draw function, handling window resises,
//ect. The draw function is really our main function now...
//main is now just used for initalitatin.

}
